import $storage from '@/utils/storage.js'
import $config from '@/utils/config.js'

const uploadFile = {
	// 上传图片
	uploadSingleImage: (sourceType)=> {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: 1,
				sizeType: ['original'],
				sourceType: [sourceType],
				success: async(res) => {
					if(!res.tempFiles && res.tempFiles.length <= 0){
						uni.$u.toast('未获取到照片')
						return;
					}
					// 开始上传文件
					const file = res.tempFiles[0]
					console.log(file)
					uni.uploadFile({
						url: $config.BASE_URL + '/chatapi/common/upload/file',
						file: file,
						name: 'file',
						filePath: file.path,
						header: {
							'authentication': $storage.getTokenStorage(),
							'x-header-client': $config.CLIENT_ID
						},
						success: (uploadResult) => {
							resolve(JSON.parse(uploadResult.data))
							return uploadResult
						},
						fail: (failResult)=> {
							resolve(failResult)
							return failResult
						}
					})
				}
			})	
		})
	}
}

export default uploadFile